<template>
  <view class="mt1em ycc pr whf">
    <image v-if="url" :src="url" class="whf" mode="aspectFit" show-menu-by-longpress />
    <button v-else @click="clk" size="mini" class="mac-ico-scan">扫一扫</button>
  </view>
</template>
<script lang="ts">
  import { defineComponent } from "vue";

  export default defineComponent({
    name: "jmp",
    data() { return { url: "" }; },
    async onLoad() {
      this.clk();
    },
    methods: {
      async clk() {
        const r = await uni.scanCode({ scanType: ["qrCode"] });
        if (r && r.result) {
          try {
            const res = await api.request({ id: r.result.replace(await getSetting("wxpub") + "?Voucher=", "") }, "voucher/use");
            if (res.data) {
              this.url = fUrl(res.data.image);
            }
          } catch (_) { }
          setTimeout(() => {
            uni.reLaunch({ url: "/pages/my" });
          }, 3000);
        }
      }
    },
  });
</script>